from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from .models import Post

#继承了syndication框架的Feed类创建了一个子类
class LatestPostsFeed(Feed):
    title = 'My blog'   #对应RSS中的<title>元素
    link = '/blog/'     #对应RSS中的<link>元素
    description = 'New posts of my blog.'  #对应RSS中的<description>元素

    def items(self):
        return Post.published.all()[:5]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return truncatewords(item.body, 30)
